package com.kajespero.dao.article.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.kajespero.dao.article.ArticleDAO;
import com.kajespero.dao.commons.impl.AbstractReadWriteDAO;
import com.kajespero.jdo.Criteria;
import com.kajespero.model.ArticleBean;
import com.kajespero.model.annotation.process.MappedValueProcess;

public class ArticleDAOImpl extends AbstractReadWriteDAO<ArticleBean, Long> implements ArticleDAO{

	@Override
	public ArticleBean createNewBean() throws InstantiationException,IllegalAccessException {
		ArticleBean article = new ArticleBean();
		article.setId(nextId());
		article.setKey(createKey(article.getId()));
		Date date = new Date();
		article.setCreateAt(date);
		article.setModifyAt(date);
		return article;
	}
	
	@Override
	public Collection<ArticleBean> findAll() {
		Criteria<ArticleBean> criteria = new Criteria<ArticleBean>(ArticleBean.class);
		criteria.addOrder("index", Criteria.DESC);
		List<ArticleBean> list = (List<ArticleBean>) criteria.executeCriteria();
		List<ArticleBean> returnList = new ArrayList<ArticleBean>();
		for (ArticleBean instance : list){
			returnList.add(MappedValueProcess.processPostLoad(instance));
		}
		return returnList;
	}
}
